<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="参数名" prop="paramKey">
        <el-input v-model="dataForm.paramKey" placeholder="参数名"></el-input>
      </el-form-item>
      <el-form-item label="参数值" prop="paramValue">
        <el-input v-model="dataForm.paramValue" placeholder="参数值"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="operate" v-if="dataForm.radio === '1'">
        <el-input v-model="dataForm.operate" placeholder="负责人"></el-input>
      </el-form-item>
      <el-form-item label="是否第三方" prop="">
        <!-- <el-radio v-model="dataForm.radio" label="1">是</el-radio>
        <el-radio v-model="dataForm.radio" label="2">否</el-radio> -->
        <el-radio-group v-model="dataForm.radio" @input="changeInput()">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="" prop="" v-if="dataForm.radio === '1'">
        <el-checkbox v-model="dataForm.checkedOne" @change="changeOne()"
          >白名单校验</el-checkbox
        >
        <el-checkbox v-model="dataForm.checkedTow" @change="changeTow()"
          >关键词校验</el-checkbox
        >
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <!-- <el-button @click="visible = false">取消</el-button> -->
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        paramKey: "",
        paramValue: "",
        remark: "",
        operate:"",//负责人
        radio: "0",
        checkedOne: false,
        checkedTow: false,
      },
      isWhite:0,//是否白名单校验
      isWord:0,//是否关键词校验
      dataRule: {
        paramKey: [
          { required: true, message: "参数名不能为空", trigger: "blur" },
        ],
        paramValue: [
          { required: true, message: "参数值不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    visible(newVal, oldVal) {
      if (newVal === false) {
        this.dataForm.radio = '0'
        this.dataForm.checkedOne = false
        this.dataForm.checkedTow = false
      }
    },
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/config/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.paramKey = data.config.paramKey;
              this.dataForm.paramValue = data.config.paramValue;
              this.dataForm.remark = data.config.remark;
              this.dataForm.operate = data.config.operate;
              // this.dataForm.radio = data.config.isTripartite;
              if(data.config.isTripartite === 1){
                this.dataForm.radio = '1'
              }else{
                this.dataForm.radio = '0'
              }
              if(data.config.isWhite === 1){
                this.dataForm.checkedOne = true
              }else{
                this.dataForm.checkedOne = false
              }
              if(data.config.isWord === 1){
                this.dataForm.checkedTow = true
              }else{
                this.dataForm.checkedTow = false
              }
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/config/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              paramKey: this.dataForm.paramKey,
              paramValue: this.dataForm.paramValue,
              remark: this.dataForm.remark,
              operate: this.dataForm.operate,
              isTripartite:this.dataForm.radio,
              isWhite:this.isWhite,
              isWord:this.isWord,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    changeInput() {
      // console.log("----", this.dataForm.radio);
      if(this.dataForm.radio === '0'){
        this.dataForm.checkedOne = false
        this.dataForm.checkedTow = false
        // this.dataForm.operate = ""
        this.isWhite = 0;
        this.isWord = 0;
      }
    },
    changeOne() {
      if (this.dataForm.checkedOne === true) {
        this.isWhite = 1;
      } else {
        this.isWhite = 0;
      }
      // console.log("----", this.dataForm.checkedOne);
      // console.log("----isWhiteisWhite", this.isWhite);
    },
    changeTow() {
      if (this.dataForm.checkedTow === true) {
        this.isWord = 1;
      } else {
        this.isWord = 0;
      }
      // console.log("----", this.dataForm.checkedTow);
      // console.log("----isWordisWord", this.isWord);
    },
  },
};
</script>
